package ua.at.tsvetkov.samizdat.activities;

import ua.at.tsvetkov.application.ActivityStack;
import ua.at.tsvetkov.debug.AcSimpleTrackingActivity;
import ua.at.tsvetkov.net.NetCheck;
import ua.at.tsvetkov.net.NetStatus;
import ua.at.tsvetkov.net.NetStatusListener;
import ua.at.tsvetkov.samizdat.Const;
import ua.at.tsvetkov.samizdat.R;
import ua.at.tsvetkov.samizdat.adapters.DataReaderAdapter;
import ua.at.tsvetkov.samizdat.data.DataDownloader;
import ua.at.tsvetkov.samizdat.data.OnCompleteLoadListener;
import ua.at.tsvetkov.ui.Rout;
import ua.at.tsvetkov.ui.menus.IconContextMenu;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ZoomControls;

public class AcTemplate extends AcSimpleTrackingActivity implements OnCancelListener {

	public static final int		DIALOG_LOAD	= 0;

	public LayoutInflater		mInflater;
	public FrameLayout			mainFrame;
	public ImageView				netStatusImage;
	public OnDismissListener	onDismissLoadDialogListener;
	public OnDismissListener	onDismissTranslateDialogListener;
	public ListView				listView;
	public IconContextMenu		iconContextMenu;
	public DataReaderAdapter	adapter;

	protected DataDownloader	dataDownloader;
	protected int					startPosition;
	protected TextView			textTitle;
	protected TextView			textInfo;
	protected TextView			textPages;
	protected TextView			textView;
	protected View					textFrameView;
	protected ZoomControls		textZoomControls;
	protected SeekBar				textSeekBar;
	protected ScrollView			textScrollView;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ac_template);
		mInflater = LayoutInflater.from(this);
		mainFrame = (FrameLayout) findViewById(R.id.AcTemplateWorkingArea);
		netStatusImage = (ImageView) findViewById(R.id.AcTemplateNetStatus);
		textTitle = (TextView) findViewById(R.id.AcTemplateTitle);
		textInfo = (TextView) findViewById(R.id.AcTemplateInfo);
		textPages = (TextView) findViewById(R.id.AcTemplatePages);
		textInfo.setVisibility(View.INVISIBLE);
		ActivityStack.add(this);
		if (NetCheck.getInstance() == null) {
			NetCheck.newInstance(this, Const.URL_SITE);
			NetCheck.getInstance().setListener(netListener());
			new Thread(NetCheck.getInstance()).start();
		} else {
			checkNetImage(NetCheck.getInstance().getStatus());
		}
	}

	/**
	 * Добавляет лист для отображения в основном фрейме
	 */
	public void addListView() {
		listView = new ListView(this);
		mainFrame.addView(listView);
		listView.setFastScrollEnabled(true);
	}

	/**
	 * Добавляет текст для отображения в основном фрейме
	 */
	public void addTextView() {
		textFrameView = LayoutInflater.from(this).inflate(R.layout.ac_book, null);
		textView = (TextView) textFrameView.findViewById(R.id.AcBookText);
		textZoomControls = (ZoomControls) textFrameView.findViewById(R.id.AcBookZoomControls);
		textSeekBar = (SeekBar) textFrameView.findViewById(R.id.AcBookSeekBar);
		textScrollView = (ScrollView) textFrameView.findViewById(R.id.AcBookScrollView);
		mainFrame.addView(textFrameView);
	}

	/**
	 * Добавляет поиск для отображения в основном фрейме
	 */
	public void addSearchView() {
	}

	@Override
	public Dialog onCreateDialog(int id) {
		super.onCreateDialog(id);
		switch (id) {
			case DIALOG_LOAD: {
				ProgressDialog progress = new ProgressDialog(this);
				progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				progress.setTitle(R.string.loading);
				progress.setMessage(this.getText(R.string.testing_connection));
				progress.setCancelable(true);
				progress.setIcon(R.drawable.icon);
				progress.setOnDismissListener(onDismissLoadDialogListener);
				progress.setOnCancelListener(this);
				progress.show();
				return progress;
			}
		}
		return null;
	}

	/**
	 * Ловит изменения в доступности сети и отображает
	 * 
	 * @return
	 */
	private NetStatusListener netListener() {
		return new NetStatusListener() {

			@Override
			public void onChengeNetStatus(NetCheck parent, final NetStatus netStatus) {
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						checkNetImage(netStatus);
					}

				});
			}
		};
	}

	/**
	 * @param netStatus
	 */
	private void checkNetImage(final NetStatus netStatus) {
		switch (netStatus) {
			case NET_OK: {
				netStatusImage.setBackgroundResource(R.drawable.ic_net_on);
				break;
			}
			case CONNECTION_MISSING: {
				netStatusImage.setBackgroundResource(R.drawable.ic_net_missing);
				break;
			}
			case NOT_DEFINED_YET: {
				netStatusImage.setBackgroundResource(R.drawable.ic_net_unknown);
				break;
			}
			default: {
				netStatusImage.setBackgroundResource(R.drawable.ic_net_off);
				break;
			}
		}
	}

	public boolean showMore(int position, ListView parent) {
		if (position == 0) {
			startPosition = startPosition - Const.LIST_VIEW_COUNT;
			if (startPosition < 0)
				startPosition = 0;
			dataDownloader.partLoadFromSD(startPosition);
			parent.setSelection(0);
			return true;
		}
		if (position == adapter.getCount() - 1) {
			if (startPosition + Const.LIST_VIEW_COUNT > dataDownloader.size())
				return true;
			startPosition = startPosition + Const.LIST_VIEW_COUNT;
			dataDownloader.partLoadFromSD(startPosition);
			parent.setSelection(0);
			return true;
		}
		return false;
	}

	/**
	 * Уведомляет адаптер об изменении данных
	 */
	public OnCompleteLoadListener	standartNotifier	= new OnCompleteLoadListener() {

																		@Override
																		public void loadComplete(boolean isLoaded) {
																			if (isLoaded) {
																				adapter.update(listView);
																			} else {
																				if (!NetCheck.getInstance().isOnline()) {
																					Rout.toast(ActivityStack.getLastActivity(), NetCheck.getInstance().getStatusStringRu(), Toast.LENGTH_LONG);
																				} else {
																					Rout.toast(ActivityStack.getLastActivity(), R.string.CONTENT_IS_ABSENT, Toast.LENGTH_LONG);
																				}
																				ActivityStack.getLastActivity().finish();
																			}
																		}

																	};

	@Override
	public void onCancel(DialogInterface arg0) {
		finish();
	}

}
